﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project2
{
    class Program
    {
        static void Main(string[] args)
        {
            AlarmClock alarmclock = new AlarmClock();
            alarmclock.setBellTime(17, 11, 10);
            alarmclock.BellEvent += new BellEventHandler(BellTrigger);
            alarmclock.Tick += new TickEventHandler(Tick);
            alarmclock.startBell();
            Console.ReadKey();
        }

        private static void BellTrigger(object sender,BellEventArgs e)//铃声触发器 写的是铃声触发时应该是什么样的
        {
            string bellMessage = string.Format("{0}时{1}分{2}秒 ---> {3}", e.Hour, e.Minute, e.Second, e.BellWords);
            Console.WriteLine(bellMessage);
            Console.WriteLine("闹铃响了！");
        }

        private static void Tick(object sender,BellEventArgs e)//时钟滴答器 写的是时钟未触发时应该是什么样的
        {
            string bellMessage = string.Format("{0}时{1}分{2}秒 ---> {3}", e.Hour, e.Minute, e.Second, e.BellWords);
            Console.WriteLine(bellMessage);
            Console.WriteLine("时钟滴答...");
         
        }
    }

    public delegate void BellEventHandler(object sender, BellEventArgs e);//闹铃事件处理器
    public delegate void TickEventHandler(object sender, BellEventArgs e);

    public class BellEventArgs : EventArgs//闹铃响起来的事件参数
    {
        public int Hour { get; }
        public int Minute { get; }
        public int Second { get; }
        public string BellWords { get; }

        public BellEventArgs(int hour, int min, int second, string BellWords)
        {
            this.Hour = hour;
            this.Minute = min;
            this.Second = second;
            this.BellWords = BellWords;
        }
    }

    public class AlarmClock//闹钟实体例子
    {
        private int hour = 0;
        private int minute = 0;
        private int second = 0;

        public event BellEventHandler BellEvent;
        public event TickEventHandler Tick;

        public void startBell()
        {
            while(DateTime.Now.Hour!=hour || DateTime.Now.Minute!=minute || DateTime.Now.Second != second)
            {
                BellEventArgs belleventargsNormal = new BellEventArgs(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, "时钟滴答...");
                Tick(this, belleventargsNormal);
            }
            BellEventArgs belleventargs = new BellEventArgs(hour,minute,second,"闹钟响了");
            BellEvent(this, belleventargs); 
        }

        public bool setBellTime(int h ,int m,int s)
        {
            if (h >= 24) return false;
            else this.hour = h;
            if (m >= 60) return false;
            else this.minute = m;
            if (s >= 60) return false;
            else this.second = s;
            return true;
        }

    }



}
